﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace POSGui.Classes.BusinessLogic
{
    public class InventoryManager
    {
        #region Singleton
        private static InventoryManager _instance;

        private InventoryManager()
        {
        }

        public static InventoryManager GetInstance()
        {
            if (_instance == null)
            {
                _instance = new InventoryManager();
            }
            return _instance;
        }
        #endregion

        #region Public Methods

        /// <summary>
        /// Extrae la lista de items
        /// </summary>
        /// <returns></returns>
        public ERPSIA.Item[] GetItems()
        {
            ERPSIA.Item[] itemsList = null;

            WebServiceAgent webServiceConnection = new WebServiceAgent();
            itemsList = webServiceConnection.GetItems();
            return itemsList;
        }

        public static ERPSIA.ItemStoreRoomInfo GetStoreInfo(ERPSIA.Item item, ERPSIA.StoreRoom store)
        {
            foreach (ERPSIA.ItemStoreRoomInfo storeInfo in item.StoreInfo)
                if (storeInfo.StoreRoom.Id == store.Id)
                    return storeInfo;
            return null;
        }

        public static double GetUnitCost(ERPSIA.Item item, ERPSIA.StoreRoom store)
        {
            foreach (ERPSIA.ItemStoreRoomInfo storeInfo in item.StoreInfo)
                if (storeInfo.StoreRoom.Id == store.Id)
                    return storeInfo.CostoUnitario;
            return 0;
        }

        public static ERPSIA.StoreRoom GetStore(int storeId, ERPSIA.StoreRoom[] stores)
        {
            foreach (ERPSIA.StoreRoom store in stores)
                if (store.Id == storeId)
                    return store;
            return null;
        }

        public static ERPSIA.Item GetItem(int itemId, ERPSIA.Item[] items)
        {
            foreach (ERPSIA.Item item in items)
                if (item.Id == itemId)
                    return item;
            return null;
        }

        public int GetCurrentStoreRoom(WebServiceAgent userAgent)
        {
            if (ClientManager.GetInstance().DefaultStore != Constants.INVALID_ID)
                return ClientManager.GetInstance().DefaultStore;

            ERPSIA.StoreRoom[] tempStores = userAgent.GetStoreRooms();

            foreach (ERPSIA.StoreRoom store in tempStores)
            {
                if (store.Name == GetDefaultStoreRoomName())
                    return store.Id;
            }

            return Constants.INVALID_ID;
        }

        private String GetDefaultStoreRoomName()
        {
            String storeRoomName = "";
            //Leer primero cual compañia se seleccionó para sacar la base de datos
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Constants.STORE_ROOM_FILE);

            XmlNodeList Bodegas = xDoc.GetElementsByTagName(Constants.TAG_STORE_ROOMS);
            XmlNodeList deposito = ((XmlElement)Bodegas[0]).GetElementsByTagName(Constants.TAG_STORE_ROOM);
            foreach (XmlNode nodo in deposito)
            {
                storeRoomName = nodo.Attributes.GetNamedItem(Constants.TAG_STORE_ROOM_NAME).InnerText;
                break;
            }

            return storeRoomName;
        }

        #endregion
    }
}